package edu.ccut.saturn.studio.dialog;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.ElementListSelectionDialog;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnSWTUtils;
import edu.ccut.saturn.studio.dialog.validatedialog.MyCellModifierValidate;
import edu.ccut.saturn.studio.dialog.validatedialog.ValidateDialog;
import edu.ccut.saturn.studio.dialog.validatedialog.ValidateLabelProvider;
import edu.ccut.saturn.studio.logic.model.Shape;
import edu.ccut.saturn.studio.logic.model.saturn.ErrorInfo;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnArg;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnParameter;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnValidate;
import edu.ccut.saturn.studio.saturndata.model.ValidateVO;

public class ParentPrimaryDialogForAdd extends Dialog {

	private TableViewer tableViewer;

	private Table table;

	private Shape shape;

	private Shell shell;

	private String namestr = "";

	private String classTypestr = "";

	private String passingTypestr = "";

	private String valuekeystr = "";

	private String scopestr = "";

	private String descriptionstr = "";

	private List<SaturnParameter> saturnParameterList;

	private List<SaturnValidate> validates = new ArrayList<SaturnValidate>();// 真数组

	private List<SaturnValidate> validatesTemp = new ArrayList<SaturnValidate>();// 假数组

	private String isValidate;

	private Text parameterIDText, parameterKeyValueText, parameterNameText,
			parameterDescriptionText;

	protected Combo parameterClassTypeCombo, parameterPassingTypeCombo,
			parameterScopeCombo;

	private Label parameterIDLabel, parameterKeyValueLabel, parameterNameLabel,
			parameterClassTypeLabel, parameterPassingTypeLabel,
			parameterScopeLabel, parameterDescriptionLabel, browser;

	private Button yesRadioButton, noRadioButton, addButton, editButton,
			deleteButton;

	protected Object result;

	private Composite containerSwitch, compositeButton;

	private Group group;

	public String id;

	private int i = 1;

	public String getIsValidate() {
		return isValidate;
	}

	public void setIsValidate(String isValidate) {
		this.isValidate = isValidate;
	}

	@SuppressWarnings("unused")
	public ParentPrimaryDialogForAdd(Shell parentShell,
			List<SaturnParameter> saturnParameterList, Shape shape) {
		super(parentShell);
		this.saturnParameterList = saturnParameterList;// 从主对话框中得到parameters数组
		this.shape = shape;// 从主对话框中得到parameters模型
		this.shell = parentShell;
	}

	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		return result;
	}

	protected void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL
				| SWT.RESIZE);
		shell.setImage(Activator.getImage(Images.SATURN_ICONS_SMALLNAME));

		final GridLayout gridLayout = new GridLayout();
		shell.setLayout(gridLayout);
		shell.setSize(500, 540);
		shell.setText(SelectionCandidate.ATTRIBUTE_SETTING);

		Composite container = new Composite(shell, SWT.NONE);
		final GridLayout gridLayout2 = new GridLayout();
		gridLayout2.numColumns = 3;
		container.setLayout(gridLayout2);

		final GridData gd_container = new GridData(SWT.FILL, SWT.BOTTOM, false,
				false);
		gd_container.horizontalIndent = -3;
		gd_container.widthHint = 471;
		gd_container.heightHint = 239;
		container.setLayoutData(gd_container);

		parameterIDLabel = new Label(container, SWT.NONE);
		final GridData gd_parameterIDLabel = new GridData(SWT.FILL, SWT.FILL,
				false, false);
		gd_parameterIDLabel.verticalIndent = 2;
		parameterIDLabel.setLayoutData(gd_parameterIDLabel);
		parameterIDLabel.setText(Messages
				.getString("ParentPrimaryDialogForAdd.ParaID")); //$NON-NLS-1$

		parameterIDText = new Text(container, SWT.READ_ONLY | SWT.BORDER);
		parameterIDText.setEditable(false);
		final GridData gridDataID2 = new GridData(SWT.FILL, SWT.TOP, true,
				false, 2, 1);
		gridDataID2.horizontalIndent = 10;
		parameterIDText.setLayoutData(gridDataID2);
		parameterIDText.setText("" + (saturnParameterList.size() + 1));

		parameterKeyValueLabel = new Label(container, SWT.NONE);
		final GridData gridDataKeyValue = new GridData();
		gridDataKeyValue.verticalIndent = 5;
		parameterKeyValueLabel.setLayoutData(gridDataKeyValue);
		parameterKeyValueLabel.setText(Messages
				.getString("ParentPrimaryDialogForAdd.expression")); //$NON-NLS-1$

		parameterKeyValueText = new Text(container, SWT.BORDER);
		parameterKeyValueText.setTextLimit(150);
		parameterKeyValueText.setFocus();
		final GridData gridDataKeyValue2 = new GridData(SWT.FILL, SWT.CENTER,
				true, false);
		gridDataKeyValue2.verticalIndent = 3;
		gridDataKeyValue2.horizontalIndent = 10;
		parameterKeyValueText.setLayoutData(gridDataKeyValue2);

		browser = new Label(container, SWT.BORDER);
		browser.setImage(Activator.getImage(Images.AMPLIFY_PLUS));
		browser.addMouseTrackListener(new MouseTrackAdapter() {
			public void mouseEnter(final MouseEvent e) {
				browser.setImage(Activator.getImage(Images.AMPLIFY_MINUS));
			}

			public void mouseExit(final MouseEvent e) {
				browser.setImage(Activator.getImage(Images.AMPLIFY_PLUS));
			}
		});
		browser.addMouseListener(new MouseAdapter() {
			public void mouseDown(final MouseEvent e) {
				ElementListSelectionDialog dialog1 = new ElementListSelectionDialog(
						shell, new SaturnSWTUtils.SaturnLabelDefaultProvider());
				dialog1
						.setTitle(Messages
								.getString("ParentPrimaryDialogForAdd.ChooseParaKeyValue")); //$NON-NLS-1$
				dialog1.setElements(shape.getParameterValueKeys());
				if (dialog1.open() == Window.OK) {
					String ddd = (String) dialog1.getFirstResult();

					parameterKeyValueText.setText(ddd);
					parameterNameText.setText(shape.getName(ddd));
					parameterClassTypeCombo.setText(shape.getClassType(ddd));
					parameterPassingTypeCombo.setText(revertToSurface(shape
							.getPassingType(ddd)));
					parameterDescriptionText.setText(shape.getDescription(ddd));
				}
			}
		});

		parameterNameLabel = new Label(container, SWT.NONE);
		final GridData gridDataName = new GridData();
		gridDataName.verticalIndent = 5;
		parameterNameLabel.setLayoutData(gridDataName);
		parameterNameLabel.setText(Messages
				.getString("ParentPrimaryDialogForAdd.ParaName")); //$NON-NLS-1$

		parameterNameText = new Text(container, SWT.BORDER);
		parameterNameText.setTextLimit(50);
		final GridData gridDataName2 = new GridData(SWT.FILL, SWT.FILL, false,
				false, 2, 1);
		gridDataName2.verticalIndent = 3;
		gridDataName2.horizontalIndent = 10;
		parameterNameText.setLayoutData(gridDataName2);
		parameterNameText.addKeyListener(new KeyAdapter() {
			public void keyPressed(final KeyEvent e) {
				if (e.character == SWT.CR) {
					buttonConfirm();
				}
			}
		});

		parameterClassTypeLabel = new Label(container, SWT.NONE);
		final GridData gridDataClassType = new GridData();
		gridDataClassType.verticalIndent = 5;
		parameterClassTypeLabel.setLayoutData(gridDataClassType);
		parameterClassTypeLabel.setText(Messages
				.getString("ParentPrimaryDialogForAdd.ParaType")); //$NON-NLS-1$

		parameterClassTypeCombo = new Combo(container, SWT.READ_ONLY);
		final GridData gridDataClassType2 = new GridData(SWT.FILL, SWT.CENTER,
				false, false, 2, 1);
		gridDataClassType2.horizontalIndent = 10;
		gridDataClassType2.verticalIndent = 3;
		parameterClassTypeCombo.setLayoutData(gridDataClassType2);
		parameterClassTypeCombo.addKeyListener(new KeyAdapter() {
			public void keyPressed(final KeyEvent e) {
				if (e.character == SWT.CR) {
					buttonConfirm();
				}
			}
		});
		parameterClassTypeCombo.setItems(SelectionCandidate.classtypes);
		parameterClassTypeCombo.select(0);

		parameterPassingTypeLabel = new Label(container, SWT.NONE);
		final GridData gridDataPassingType = new GridData();
		gridDataPassingType.verticalIndent = 5;
		parameterPassingTypeLabel.setLayoutData(gridDataPassingType);
		parameterPassingTypeLabel.setText(Messages
				.getString("ParentPrimaryDialogForAdd.InputOutput")); //$NON-NLS-1$

		parameterPassingTypeCombo = new Combo(container, SWT.READ_ONLY);
		final GridData gridDataPassingType2 = new GridData(SWT.FILL,
				SWT.CENTER, true, false, 2, 1);
		gridDataPassingType2.horizontalIndent = 10;
		gridDataPassingType2.verticalIndent = 3;
		parameterPassingTypeCombo.setLayoutData(gridDataPassingType2);
		parameterPassingTypeCombo.addKeyListener(new KeyAdapter() {
			public void keyPressed(final KeyEvent e) {
				if (e.character == SWT.CR) {
					buttonConfirm();
				}
			}
		});
		parameterPassingTypeCombo.setItems(SelectionCandidate.passingTypes);
		parameterPassingTypeCombo.select(0);

		parameterScopeLabel = new Label(container, SWT.CENTER);
		final GridData gridDataScope = new GridData();
		gridDataScope.verticalIndent = 5;
		parameterScopeLabel.setLayoutData(gridDataScope);
		parameterScopeLabel.setText(Messages
				.getString("ParentPrimaryDialogForAdd.DataDomain")); //$NON-NLS-1$

		parameterScopeCombo = new Combo(container, SWT.BORDER | SWT.READ_ONLY);
		final GridData gridDataScope2 = new GridData(SWT.FILL, SWT.CENTER,
				false, false, 2, 1);
		gridDataScope2.horizontalIndent = 10;
		gridDataScope2.verticalIndent = 3;
		parameterScopeCombo.setLayoutData(gridDataScope2);
		parameterScopeCombo.addKeyListener(new KeyAdapter() {
			public void keyPressed(final KeyEvent e) {
				if (e.character == SWT.CR) {
					buttonConfirm();
				}
			}
		});

		iniScope(parameterScopeCombo);
		parameterScopeCombo.select(0);

		parameterDescriptionLabel = new Label(container, SWT.WRAP);
		final GridData gridDataDescription = new GridData(SWT.LEFT, SWT.TOP,
				false, false);
		gridDataDescription.verticalIndent = 5;
		gridDataDescription.widthHint = 80;
		parameterDescriptionLabel.setLayoutData(gridDataDescription);
		parameterDescriptionLabel.setText(Messages
				.getString("ParentPrimaryDialogForAdd.ParaDescription")); //$NON-NLS-1$
		parameterDescriptionLabel.pack();

		parameterDescriptionText = new Text(container, SWT.V_SCROLL
				| SWT.BORDER);
		parameterDescriptionText.setTextLimit(200);
		final FormData fd_descripiontext = new FormData();
		fd_descripiontext.right = new FormAttachment(parameterScopeCombo, 0,
				SWT.RIGHT);
		fd_descripiontext.left = new FormAttachment(20, 0);
		fd_descripiontext.bottom = new FormAttachment(0, 255);
		fd_descripiontext.top = new FormAttachment(parameterDescriptionLabel,
				0, SWT.TOP);
		parameterDescriptionText.setLayoutData(fd_descripiontext);

		final GridData gridDataDescription2 = new GridData(SWT.FILL,
				SWT.CENTER, false, false, 2, 1);
		gridDataDescription2.widthHint = 139;
		gridDataDescription2.verticalIndent = 3;
		gridDataDescription2.horizontalIndent = 10;
		gridDataDescription2.heightHint = 57;
		parameterDescriptionText.setLayoutData(gridDataDescription2);

		group = new Group(shell, SWT.NONE);
		final GridData gd_group = new GridData();
		gd_group.horizontalIndent = 3;
		group.setLayoutData(gd_group);
		group.setText(Messages
				.getString("ParentPrimaryDialogForAdd.ApplicationValidation")); //$NON-NLS-1$
		group.setLayout(new FormLayout());

		yesRadioButton = new Button(group, SWT.RADIO);
		yesRadioButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				containerSwitch.setVisible(true);
				shell.setSize(500, 540);
			}
		});
		final FormData fd_addRadioButton = new FormData();
		fd_addRadioButton.right = new FormAttachment(0, 63);
		fd_addRadioButton.left = new FormAttachment(0, 15);
		yesRadioButton.setLayoutData(fd_addRadioButton);
		yesRadioButton.setText(Messages
				.getString("ParentPrimaryDialogForAdd.yes")); //$NON-NLS-1$

		noRadioButton = new Button(group, SWT.RADIO);
		noRadioButton.setSelection(true);
		noRadioButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				containerSwitch.setVisible(false);
				shell.setSize(484, 400);
			}
		});

		fd_addRadioButton.top = new FormAttachment(noRadioButton, 0, SWT.TOP);
		final FormData fd_notAddRadioButton = new FormData();
		fd_notAddRadioButton.bottom = new FormAttachment(0, 21);
		fd_notAddRadioButton.top = new FormAttachment(0, 5);
		fd_notAddRadioButton.right = new FormAttachment(0, 150);
		fd_notAddRadioButton.left = new FormAttachment(0, 100);
		noRadioButton.setLayoutData(fd_notAddRadioButton);
		noRadioButton.setText(Messages
				.getString("ParentPrimaryDialogForAdd.no")); //$NON-NLS-1$

		containerSwitch = new Composite(shell, SWT.NONE);
		final GridData gd_compositeTable = new GridData(SWT.FILL, SWT.FILL,
				true, true);
		gd_compositeTable.horizontalIndent = 3;
		gd_compositeTable.heightHint = 118;
		containerSwitch.setLayoutData(gd_compositeTable);
		containerSwitch.setLayout(new FormLayout());

		tableViewer = new TableViewer(containerSwitch, SWT.MULTI
				| SWT.FULL_SELECTION | SWT.BORDER);
		tableViewer.setContentProvider(new TableViewerContentProvider());
		tableViewer.setLabelProvider(new ValidateLabelProvider());
		table = tableViewer.getTable();
		final FormData fd_table = new FormData();
		fd_table.bottom = new FormAttachment(100, -20);
		fd_table.right = new FormAttachment(100, -50);
		fd_table.top = new FormAttachment(0, 2);
		fd_table.left = new FormAttachment(0, 0);
		table.setLayoutData(fd_table);
		table.setLinesVisible(true);
		table.setHeaderVisible(true);

		// 让表格可以直接编辑
		tableViewer.setColumnProperties(new String[] { "id", "key", "name",
				"class", "JS" });
		CellEditor[] cellEditor = new CellEditor[5];
		cellEditor[0] = null;
		cellEditor[1] = new TextCellEditor(tableViewer.getTable());
		cellEditor[2] = new TextCellEditor(tableViewer.getTable());
		cellEditor[3] = new TextCellEditor(tableViewer.getTable());
		cellEditor[4] = new TextCellEditor(tableViewer.getTable());

		tableViewer.setCellEditors(cellEditor);
		tableViewer.setCellModifier(new MyCellModifierValidate(tableViewer));

		final TableColumn tableColumnID = new TableColumn(table, SWT.NONE);
		tableColumnID.setWidth(60);
		tableColumnID.setText(Messages
				.getString("ParentPrimaryDialogForAdd.RunningNumber")); //$NON-NLS-1$

		final TableColumn tableColumnKey = new TableColumn(table, SWT.NONE);
		tableColumnKey.setWidth(95);
		tableColumnKey.setText(Messages
				.getString("ParentPrimaryDialogForAdd.ValidateID")); //$NON-NLS-1$

		final TableColumn tableColumnName = new TableColumn(table, SWT.NONE);
		tableColumnName.setWidth(90);
		tableColumnName.setText(Messages
				.getString("ParentPrimaryDialogForAdd.ValidateName")); //$NON-NLS-1$

		final TableColumn tableColumnClass = new TableColumn(table, SWT.NONE);
		tableColumnClass.setWidth(90);
		tableColumnClass.setText(Messages
				.getString("ParentPrimaryDialogForAdd.ValidateClass")); //$NON-NLS-1$

		final TableColumn tableColumnJS = new TableColumn(table, SWT.NONE);
		tableColumnJS.setWidth(90);
		tableColumnJS.setText(Messages
				.getString("ParentPrimaryDialogForAdd.JsValidate")); //$NON-NLS-1$

		addButton = new Button(containerSwitch, SWT.NONE);
		addButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				addValidate();// //////////////////////////////////////添加validate
			}
		});
		final FormData fd_addButton = new FormData();
		fd_addButton.top = new FormAttachment(0, 2);
		fd_addButton.bottom = new FormAttachment(0, 25);
		fd_addButton.left = new FormAttachment(100, -41);
		fd_addButton.right = new FormAttachment(100, -5);
		addButton.setLayoutData(fd_addButton);
		addButton.setText(SelectionCandidate.ADD_BUTTON);

		editButton = new Button(containerSwitch, SWT.NONE);
		editButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				modValidate();// ////////////////////////////////////修改validate
			}
		});
		final FormData fd_editButton = new FormData();
		fd_editButton.top = new FormAttachment(0, 32);
		fd_editButton.bottom = new FormAttachment(0, 55);
		fd_editButton.right = new FormAttachment(addButton, 0, SWT.RIGHT);
		fd_editButton.left = new FormAttachment(addButton, 0, SWT.LEFT);
		editButton.setLayoutData(fd_editButton);
		editButton.setText(SelectionCandidate.EDIT_BUTTON);

		deleteButton = new Button(containerSwitch, SWT.NONE);
		deleteButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				delValidate();// /////////////////////////////////////删除alidate
			}
		});
		final FormData fd_deleteButton = new FormData();
		fd_deleteButton.right = new FormAttachment(editButton, 0, SWT.RIGHT);
		fd_deleteButton.left = new FormAttachment(editButton, 0, SWT.LEFT);
		fd_deleteButton.bottom = new FormAttachment(editButton, 31, SWT.BOTTOM);
		fd_deleteButton.top = new FormAttachment(editButton, 8, SWT.BOTTOM);
		deleteButton.setLayoutData(fd_deleteButton);
		deleteButton.setText(SelectionCandidate.DELETE_BUTTON);

		final Button buttonUP = new Button(containerSwitch, SWT.NONE);
		buttonUP.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				IStructuredSelection selection = (IStructuredSelection) tableViewer
						.getSelection();
				if (selection.isEmpty()) {
					MessageDialog.openInformation(null,
							SelectionCandidate.HINT,
							SelectionCandidate.CHOOSE_EDIT_RECORD);
				} else {
					SaturnValidate validate = (SaturnValidate) selection
							.getFirstElement();
					List<SaturnValidate> list = (List<SaturnValidate>) tableViewer
							.getInput();
					for (int i = 0; i < list.size(); i++) {
						if (list.get(i).hashCode() == validate.hashCode()) {
							if (i - 1 != -1) {
								list.remove(i);
								list.add(i - 1, validate);
							}
						}
					}
					sortValidates(list);
					tableViewer.setInput(list);
					validatesTemp = list;
				}
			}
		});
		final FormData fd_buttonUP = new FormData();
		fd_buttonUP.top = new FormAttachment(0, 93);
		fd_buttonUP.bottom = new FormAttachment(0, 120);
		fd_buttonUP.left = new FormAttachment(deleteButton, 0, SWT.LEFT);
		buttonUP.setLayoutData(fd_buttonUP);
		buttonUP.setText(Messages.getString("ParentPrimaryDialogForAdd.UpShift"));  //$NON-NLS-1$

		final Button buttonDown = new Button(containerSwitch, SWT.NONE);
		buttonDown.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				IStructuredSelection selection = (IStructuredSelection) tableViewer
						.getSelection();
				if (selection.isEmpty()) {
					MessageDialog.openInformation(null,
							SelectionCandidate.HINT,
							SelectionCandidate.CHOOSE_EDIT_RECORD);
				} else {
					SaturnValidate validate = (SaturnValidate) selection
							.getFirstElement();
					List<SaturnValidate> list = (List<SaturnValidate>) tableViewer
							.getInput();
					for (int i = 0; i < list.size(); i++) {
						if (list.get(i).hashCode() == validate.hashCode()) {
							if (i + 1 < list.size()) {
								list.remove(i);
								list.add(i + 1, validate);
								break;
							}
						}
					}
					sortValidates(list);
					tableViewer.setInput(list);
					validatesTemp = list;
				}
			}
		});
		final FormData fd_buttonDown = new FormData();
		fd_buttonDown.top = new FormAttachment(buttonUP, 5, SWT.BOTTOM);
		fd_buttonDown.left = new FormAttachment(buttonUP, 0, SWT.LEFT);
		buttonDown.setLayoutData(fd_buttonDown);
		buttonDown.setText(Messages.getString("ParentPrimaryDialogForAdd.DownShift"));  //$NON-NLS-1$
		compositeButton = new Composite(shell, SWT.NONE);
		final GridData gd_compositeButtion = new GridData(SWT.FILL, SWT.FILL,
				false, false);
		gd_compositeButtion.widthHint = 469;
		compositeButton.setLayoutData(gd_compositeButtion);
		compositeButton.setLayout(new FormLayout());

		Button comfirmButton = new Button(compositeButton, SWT.NONE);
		comfirmButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				setI(0);
				buttonConfirm();
			}
		});
		final FormData fd_buttonYes = new FormData();
		fd_buttonYes.left = new FormAttachment(0, 305);
		comfirmButton.setLayoutData(fd_buttonYes);
		comfirmButton.setText("OK");

		Button cancelButton;
		cancelButton = new Button(compositeButton, SWT.NONE);
		fd_buttonYes.right = new FormAttachment(cancelButton, -5, SWT.LEFT);
		fd_buttonYes.bottom = new FormAttachment(cancelButton, 23, SWT.TOP);
		fd_buttonYes.top = new FormAttachment(cancelButton, 0, SWT.TOP);
		cancelButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				setI(1);
				shell.dispose();
			}
		});
		final FormData fd_buttonNo = new FormData();
		fd_buttonNo.left = new FormAttachment(0, 385);
		fd_buttonNo.right = new FormAttachment(0, 460);
		fd_buttonNo.top = new FormAttachment(comfirmButton, 0, SWT.TOP);
		cancelButton.setLayoutData(fd_buttonNo);
		cancelButton.setText("Cancel");

		containerSwitch.setVisible(false);
		shell.setSize(484, 400);
	}

	/**
	 * Create contents of the button bar
	 * 
	 * @param parent
	 */
	private void buttonConfirm() {
		namestr = parameterNameText.getText();
		classTypestr = parameterClassTypeCombo.getText();
		passingTypestr = parameterPassingTypeCombo.getText();
		valuekeystr = parameterKeyValueText.getText();
		scopestr = parameterScopeCombo.getText();
		descriptionstr = parameterDescriptionText.getText();
		if (this.yesRadioButton.getSelection()) {
			setIsValidate("true");
		} else {
			setIsValidate("false");
		}
		setValidates(validates);
		this.shell.dispose();
	}

	private void sortValidates(List<SaturnValidate> list) {
		for (int i = 0; i < list.size(); i++) {
			list.get(i).setId("" + (i + 1));
		}
	}

	// 将文本框值赋值给MethodVO，供主页面取值
	public void addList(List<SaturnParameter> saturnParameterList) {
		SaturnParameter parameterVo = new SaturnParameter();
		parameterVo.setId("" + (saturnParameterList.size() + 1));
		parameterVo.setName(namestr);
		parameterVo.setClassType(classTypestr);
		parameterVo.setPassingType(passingTypestr);
		parameterVo.setValueKey(valuekeystr);
		parameterVo.setScope(scopestr);
		parameterVo.setDescription(descriptionstr);
		parameterVo.setValidates(getValidates());
		parameterVo.setIsValidate(getIsValidate());
		saturnParameterList.add(parameterVo);
	}

	private String revertToSurface(String aa) {
		if (aa.equals("in") || aa.equals(SelectionCandidate.passingTypes[0]))
			return SelectionCandidate.passingTypes[0];
		else if (aa.equals("out")
				|| aa.equals(SelectionCandidate.passingTypes[1]))
			return SelectionCandidate.passingTypes[1];
		else if (aa.equals("in-out")
				|| aa.equals(SelectionCandidate.passingTypes[2]))
			return SelectionCandidate.passingTypes[2];
		return "";
	}

	public String getNamestr() {
		return namestr;
	}

	public void setNamestr(String namestr) {
		this.namestr = namestr;
	}

	public String getClassTypestr() {
		return classTypestr;
	}

	public void setClassTypestr(String classTypestr) {
		this.classTypestr = classTypestr;
	}

	public String getPassingTypestr() {
		return passingTypestr;
	}

	public void setPassingTypestr(String passingTypestr) {
		this.passingTypestr = passingTypestr;
	}

	public String getValuekeystr() {
		return valuekeystr;
	}

	public void setValuekeystr(String valuekeystr) {
		this.valuekeystr = valuekeystr;
	}

	public String getScopestr() {
		return scopestr;
	}

	public void setScopestr(String scopestr) {
		this.scopestr = scopestr;
	}

	public String getDescriptionstr() {
		return descriptionstr;
	}

	public void setDescriptionstr(String descriptionstr) {
		this.descriptionstr = descriptionstr;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	private void addValidate() {
		ValidateVO validateVO = null;
		ValidateDialog vali = new ValidateDialog(shell, validateVO, shape);// 打开添加对话框，传vali数组和模型

		vali.open();
		validateVO = vali.getValidateVO();
		if (validateVO == null) {
			return;
		}

		tableViewer.setInput(transfer(validates, validateVO));
		parameterKeyValueText.setFocus();
	}

	@SuppressWarnings("unchecked")
	private List<SaturnValidate> transfer(List<SaturnValidate> validates,
			ValidateVO validateVO) {
		SaturnValidate volists = new SaturnValidate();
		List<SaturnArg> args = new ArrayList<SaturnArg>();
		List<ErrorInfo> errs = new ArrayList<ErrorInfo>();
		
		
		volists.setId(String.valueOf(i++));
		volists.setKey(validateVO.getKey());
		volists.setName(validateVO.getName());
		volists.setValidateClass(validateVO.getClassTypeStr());
		volists.setJsMethodName(validateVO.getJSMethodName());
		volists.setArgs(transferArgs(validateVO.getArgs(), args));
		volists.setDescription(validateVO.getDescription());
		volists.setEventInfos(validateVO.getEventInfos());
		volists.setValidateType(validateVO.getValidateType());
		volists.setErrorInfos(transferErrs(validateVO.getErrorInfos(),errs));
		validatesTemp.add(volists);

		return this.validates = validatesTemp;
	}
		
	private List<ErrorInfo> transferErrs (Map<String, String> errs1, List<ErrorInfo> errs2) {
		int i = 0;
		for(; i < errs1.size(); i++) {
			ErrorInfo err = new ErrorInfo();
			err.setKey((String) errs1.keySet().toArray()[i]);
			err.setValue((String) errs1.values().toArray()[i]);
			errs2.add(err);
		}
		return errs2;
	}
	
	private List<SaturnArg> transferArgs (List<String> args1, List<SaturnArg> args2) {
		int i = 0;
		
		for (String arg : args1) {
				SaturnArg arg1 = new SaturnArg();
					arg1.setArg(arg);
					arg1.setId(String.valueOf(i++));
					args2.add(arg1);
		}
		return args2;
	}
	
	private void modValidate() {
		IStructuredSelection selection = (IStructuredSelection) tableViewer
				.getSelection();
		if (selection.isEmpty()) {
			MessageDialog.openInformation(null, SelectionCandidate.HINT,
					SelectionCandidate.CHOOSE_EDIT_RECORD);
		} else {
			ValidateVO validateVO = null;
			int j = tableViewer.getTable().getSelectionIndex();
			i = ++j;
			ValidateDialog vali = new ValidateDialog(shell, validateVO, shape);
			vali.open();
			validateVO = vali.getValidateVO();
			validates.remove(tableViewer.getTable().getSelectionIndex());
			validates = transfer(validates, validateVO);
			this.reorder(validates);
			tableViewer.setInput(validates);
			i = tableViewer.getTable().getItemCount() + 1;
			parameterKeyValueText.setFocus();
		}
	}

	private void delValidate() {
		IStructuredSelection selection = (IStructuredSelection) tableViewer
				.getSelection();
		if (selection.isEmpty()) {
			MessageDialog.openInformation(null, SelectionCandidate.HINT,
					SelectionCandidate.CHOOSE_DELETE_RECORD);
		} else {
			validates.remove(tableViewer.getTable().getSelectionIndex());
			this.reorder(validates);
			tableViewer.setInput(validates);
			i = tableViewer.getTable().getItemCount() + 1;
			parameterKeyValueText.setFocus();
		}
	}

	private void reorder(List<SaturnValidate> parameters) {

		if (parameters != null) {
			Collections.sort(parameters);
			for (int i = 0; i < parameters.size(); i++) {
				parameters.get(i).setId("" + (i + 1));
			}
		}
	}

	public List<SaturnValidate> getValidates() {
		return validates;
	}

	public void setValidates(List<SaturnValidate> validates) {
		this.validates = validates;
	}

	public int getI() {
		return i;
	}

	public void setI(int i) {
		this.i = i;
	}

	protected void iniScope(Combo combo) {
		combo.setItems(SelectionCandidate.scopes);
	}


}
